package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.qo.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        // 新增逻辑
        // 状态为预约中
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        // 到店时间为null
        appointment.setActualArrivalTime(null);
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment oldObj = get(appointment.getId());
        // 编辑逻辑
        // 只有状态为预约中才能编辑
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
            throw new BusinessException("只有状态为预约中才能编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }



    // 到店
    @Override
    public void arrival(Long id) {
        Appointment appointment = get(id);
        // 状态为到店
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有状态为预约中才能选择到店");
        }
        appointmentMapper.arrival(Appointment.STATUS_ARRIVAL,new Date(),id);
    }

    // 取消预约
    @Override
    public void cancel(Long id, Integer statusCancel) {
        Appointment oldObj = get(id);
        // 只有状态为预约中才能编辑
        if (!Appointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())){
            throw new BusinessException("只有状态为预约中才能取消预约");
        }
        appointmentMapper.changeStatus(id,statusCancel);
    }

    // 删除
    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            Appointment appointment = get(dictId);
            if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
                throw new BusinessException("只有状态为预约中才能删除");
            }
            appointmentMapper.falseDelete(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    @Override
    public void updateStatus(Long appointmentId, Integer status) {
        appointmentMapper.changeStatus(appointmentId,status);
    }

    // 结账单
    @Override
    public Statement generateStatement(Long appointmentId) {
        Appointment oldObj = get(appointmentId);
        // 已到店 和 已结算 才可以点结算
        if (!(Appointment.STATUS_ARRIVAL.equals(oldObj.getStatus())
                || Appointment.STATUS_SETTLE.equals(oldObj.getStatus())))
        {
            throw new BusinessException("已到店和已结算才可以点结算");
        }
        // 根据预约单ID查询对应结算单对象
        Statement statement = statementService.getByAppointmentId(appointmentId);
        if (statement == null){
            // 说明是通过预约单生成的结算单
            statement = new Statement();
            // 关联预约单ID 只有通过预约单生成的结算单才有 appointmentId
            statement.setAppointmentId(appointmentId);
            // 关联预约单数据
            statement.setCarSeries(oldObj.getCarSeries());
            statement.setActualArrivalTime(oldObj.getActualArrivalTime());
            statement.setInfo(oldObj.getInfo());
            statement.setCreateTime(new Date());
            statement.setCustomerName(oldObj.getCustomerName());
            statement.setCustomerPhone(oldObj.getCustomerPhone());
            statement.setServiceType(oldObj.getServiceType());
            statement.setLicensePlate(oldObj.getLicensePlate());
            // 生成结算单
            statementService.save(statement);
        }
        return statement;
    }
}

